"use strict";

import Vue from 'vue';
import axios from "axios";
import Message from 'element-ui'

import router from "@/router";
// 导入NProgresss对应的js和css
import NProgresss from 'nprogress'
import 'nprogress/nprogress.css'
// axios.defaults.withCredentials = true; // 支持跨域访问
// 为post请求设置请求头
axios.defaults.headers.post['Content-Type'] = 'multipart/form-data';
// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.baseURL = "http://47.109.16.23:5566/api"
// axios.defaults.baseURL = "http://125.66.0.11:5566/api"
// axios.defaults.baseURL = "http://125.66.0.11:5566/api"
// 公网环境后端配置
axios.defaults.baseURL = "http://125.66.0.11:8090/wages/api"
// axios.defaults.baseURL = "http://192.168.5.81:5566/api"
// axios.defaults.baseURL = "http://47.109.16.23:5566/api"
const cancelToken = axios.CancelToken
const source = cancelToken.source()

let config = {
    headers: {
        'Content-Type': "application/json; charset=utf-8"
    },
    // baseURL: process.env.baseURL || process.env.apiUrl || ""
    timeout: 60 * 1000*30, // Timeout

};
let loading = false
let loadingCount = 0


function openLoading(loadingDom) {
    loading = Vue.prototype.$loading({
        lock: true,
        text: '加载中，请稍候...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.5)',
        target: loadingDom || 'body'
    })
}

function closeLoading() {
    loading && loading.close()
}

export function startLoading() {
    // if (loadingCount === 0) {
        openLoading()
        // loadingCount++
    // }
}

export function endLoading() {
    // loadingCount--
    // if (loadingCount === 0) {
        closeLoading()
    // }
}

const _axios = axios.create(config);

_axios.interceptors.request.use(
    function (config) {
        NProgresss.start()
        // Do something before request is sent
        config.headers['Authorization'] = window.localStorage.getItem("token")
        // config.headers.Authorization=localStorage.getItem("token")
        // startLoading()
        // openLoading()
        return config;
    },
    function (error) {
        // Do something with request error
        return Promise.reject(error);
    }
);

// Add a response interceptor
_axios.interceptors.response.use(
    function (response) {
        // Do something with response data

        if (response.data.code === 201) {
            source.cancel('登录信息已过期'); // 取消其他正在进行的请求
            Message.MessageBox({
                type: "error",
                title: '警告',
                message: '登录过期，请重新登录'
            }).then(() => {
                // window.location.href="/login"
                router.push("/login")
                window.location.reload()
            });
            return ;
        }else{
            if (response.status === 200) {
                console.log("关闭")
                // setTimeout(endLoading, 100)
                // closeLoading()
            }
        }
        NProgresss.done()
        return response;
    },
    function (error) {
        // router.push("/error")
        // Do something with response error
        if (axios.isCancel(error)) { // 取消请求的情况下，终端Promise调用链
            return new Promise(() => {
            });
        } else {

            return Promise.reject(error);
        }

    }
);

Plugin.install = function (Vue, options) {
    Vue.axios = _axios;
    window.axios = _axios;
    Object.defineProperties(Vue.prototype, {
        axios: {
            get() {
                return _axios;
            }
        },
        $axios: {
            get() {
                return _axios;
            }
        },
    });
};

Vue.use(Plugin)

export default Plugin;
